Skip to contentMethod: lambda$removeObjectPropertyAssertions$5(OWLObjectProperty, OWLNamedIndividual, Value)
1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: * <p>
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.ontodriver.owlapi;
16:
17: import cz.cvut.kbss.ontodriver.descriptor.AxiomDescriptor;
18: import cz.cvut.kbss.ontodriver.model.Assertion;
19: import cz.cvut.kbss.ontodriver.model.NamedResource;
20: import cz.cvut.kbss.ontodriver.model.Value;
21: import cz.cvut.kbss.ontodriver.owlapi.connector.OntologySnapshot;
22: import cz.cvut.kbss.ontodriver.owlapi.util.MutableRemoveAxiom;
23: import cz.cvut.kbss.ontodriver.owlapi.util.OwlapiUtils;
24: import org.semanticweb.owlapi.model.*;
25: import org.semanticweb.owlapi.search.EntitySearcher;
26:
27: import java.util.*;
28: import java.util.stream.Collectors;
29: import java.util.stream.Stream;
30:
31: class EpistemicAxiomRemover {
32:
33: private final OwlapiAdapter owlapiAdapter;
34: private final OWLOntology ontology;
35: private final OWLDataFactory dataFactory;
36: private final OntologySnapshot snapshot;
37:
38: EpistemicAxiomRemover(OwlapiAdapter adapter, OntologySnapshot snapshot) {
39: this.owlapiAdapter = adapter;
40: this.snapshot = snapshot;
41: this.ontology = snapshot.getOntology();
42: this.dataFactory = snapshot.getDataFactory();
43: }
44:
45: void remove(AxiomDescriptor descriptor) {
46: final List<OWLOntologyChange> changes = new ArrayList<>();
47: final OWLNamedIndividual individual = OwlapiUtils.getIndividual(descriptor.getSubject(), dataFactory);
48: for (Assertion a : descriptor.getAssertions()) {
49: switch (a.getType()) {
50: case CLASS:
51: changes.addAll(removeClassAssertionAxioms(individual));
52: break;
53: case DATA_PROPERTY:
54: changes.addAll(removeDataPropertyAssertions(individual, a));
55: break;
56: case OBJECT_PROPERTY:
57: changes.addAll(removeObjectPropertyAssertions(individual, a));
58: break;
59: case ANNOTATION_PROPERTY:
60: changes.addAll(removeAnnotationAssertions(individual, a));
61: }
62: }
63: if (!changes.isEmpty()) {
64: owlapiAdapter.addTransactionalChanges(snapshot.applyChanges(changes));
65: }
66: }
67:
68: private Collection<OWLOntologyChange> removeClassAssertionAxioms(OWLNamedIndividual individual) {
69: return EntitySearcher.getTypes(individual, ontology).map(cls -> new MutableRemoveAxiom(ontology,
70: dataFactory.getOWLClassAssertionAxiom(cls, individual))).collect(Collectors.toList());
71: }
72:
73: private Collection<OWLOntologyChange> removeClassAssertionAxioms(OWLNamedIndividual individual,
74: Set<Value<?>> values) {
75: return values.stream().map(value -> {
76: final OWLClass owlClass = dataFactory.getOWLClass(IRI.create(value.stringValue()));
77: return new MutableRemoveAxiom(ontology, dataFactory.getOWLClassAssertionAxiom(owlClass, individual));
78: }).collect(Collectors.toList());
79: }
80:
81: private Collection<? extends OWLOntologyChange> removeDataPropertyAssertions(OWLNamedIndividual individual,
82: Assertion assertion) {
83: final OWLDataProperty dataProperty = dataFactory.getOWLDataProperty(IRI.create(assertion.getIdentifier()));
84: final Stream<OWLLiteral> values = EntitySearcher.getDataPropertyValues(individual, dataProperty, ontology);
85: return values.map(value -> new MutableRemoveAxiom(ontology,
86: dataFactory.getOWLDataPropertyAssertionAxiom(dataProperty, individual, value)))
87: .collect(Collectors.toList());
88: }
89:
90: private Collection<? extends OWLOntologyChange> removeDataPropertyAssertions(OWLNamedIndividual individual,
91: IRI propertyId,
92: Set<Value<?>> values) {
93: final OWLDataProperty dataProperty = dataFactory.getOWLDataProperty(propertyId);
94: return values.stream().map(value -> {
95: final OWLLiteral literal = OwlapiUtils
96: .createOWLLiteralFromValue(value.getValue(), dataFactory, owlapiAdapter.getLanguage());
97: return new MutableRemoveAxiom(ontology,
98: dataFactory.getOWLDataPropertyAssertionAxiom(dataProperty, individual, literal));
99: }).collect(Collectors.toList());
100: }
101:
102: private Collection<? extends OWLOntologyChange> removeObjectPropertyAssertions(OWLNamedIndividual individual,
103: Assertion assertion) {
104: final OWLObjectProperty objProperty = dataFactory.getOWLObjectProperty(IRI.create(assertion.getIdentifier()));
105: final Stream<OWLIndividual> values = EntitySearcher.getObjectPropertyValues(individual, objProperty, ontology);
106: return values.filter(OWLIndividual::isNamed).map(value -> new MutableRemoveAxiom(ontology,
107: dataFactory.getOWLObjectPropertyAssertionAxiom(objProperty, individual, value)))
108: .collect(Collectors.toList());
109: }
110:
111: private Collection<? extends OWLOntologyChange> removeObjectPropertyAssertions(OWLNamedIndividual individual,
112: IRI assertionId,
113: Set<Value<?>> values) {
114: final OWLObjectProperty objProperty = dataFactory.getOWLObjectProperty(assertionId);
115: return values.stream().map(value -> {
116: final OWLIndividual object = OwlapiUtils
117: .getIndividual(NamedResource.create(value.stringValue()), dataFactory);
118: return new MutableRemoveAxiom(ontology,
119: dataFactory.getOWLObjectPropertyAssertionAxiom(objProperty, individual, object));
120: }).collect(Collectors.toList());
121: }
122:
123: private Collection<? extends OWLOntologyChange> removeAnnotationAssertions(OWLNamedIndividual individual,
124: Assertion assertion) {
125: final OWLAnnotationProperty annProperty = dataFactory
126: .getOWLAnnotationProperty(IRI.create(assertion.getIdentifier()));
127: final Stream<OWLAnnotationAssertionAxiom> values =
128: EntitySearcher.getAnnotationAssertionAxioms(individual.getIRI(), ontology);
129: return values.filter(axiom -> axiom.getProperty().equals(annProperty))
130: .map(value -> new MutableRemoveAxiom(ontology, value)).collect(Collectors.toList());
131: }
132:
133: private Collection<? extends OWLOntologyChange> removeAnnotationAssertions(OWLNamedIndividual individual,
134: IRI assertionId,
135: Set<Value<?>> values) {
136: final OWLAnnotationProperty annProperty = dataFactory.getOWLAnnotationProperty(assertionId);
137: return values.stream().map(value -> {
138: OWLAnnotationValue av;
139: try {
140: av = IRI.create(value.stringValue());
141: } catch (IllegalArgumentException e) {
142: av = OwlapiUtils.createOWLLiteralFromValue(value.getValue(), dataFactory, owlapiAdapter.getLanguage());
143: }
144: return new MutableRemoveAxiom(ontology,
145: dataFactory.getOWLAnnotationAssertionAxiom(annProperty, individual.getIRI(), av));
146: }).collect(Collectors.toList());
147: }
148:
149: void removeAxioms(NamedResource subject, Map<Assertion, Set<Value<?>>> toRemove) {
150: final List<OWLOntologyChange> changes = new ArrayList<>();
151: final OWLNamedIndividual individual = OwlapiUtils.getIndividual(subject, dataFactory);
152: for (Map.Entry<Assertion, Set<Value<?>>> e : toRemove.entrySet()) {
153: final IRI assertionIri = IRI.create(e.getKey().getIdentifier());
154: if (ontology.containsDataPropertyInSignature(assertionIri)) {
155: changes.addAll(removeDataPropertyAssertions(individual, assertionIri, e.getValue()));
156: } else if (ontology.containsObjectPropertyInSignature(assertionIri)) {
157: changes.addAll(removeObjectPropertyAssertions(individual, assertionIri, e.getValue()));
158: } else if (ontology.containsAnnotationPropertyInSignature(assertionIri)) {
159: changes.addAll(removeAnnotationAssertions(individual, assertionIri, e.getValue()));
160: } else if (e.getKey().isClassAssertion()) {
161: changes.addAll(removeClassAssertionAxioms(individual, e.getValue()));
162: }
163: // It can happen that the assertionIri is no longer in the ontology, because of the way properties changes
164: // are processed in JOPA - they are compared to the original object, so if multiple property values are removed
165: // in a transaction, they are effectively removed multiple times. Therefore, it can happen that another
166: // property no longer exists in the ontology, because it was removed in the previous modifications during the same
167: // transaction
168: }
169: owlapiAdapter.addTransactionalChanges(snapshot.applyChanges(changes));
170: }
171: }